#include "components.h"

#include <iostream>

#include <util/gref.h>

// models
#include <models/model_action_container.h>
#include <models/model_console.h>
#include <models/model_mesh.h>
// widgets
#include <widgets/main_frame.h>
#include <widgets/mesh_widget.h>
#include <widgets/navigation_widget.h>
#include <widgets/palette_widget.h>
#include <widgets/surface_widget.h>


// model formats
//#include <core/formats/gmsh/gmsh.h>
//#include <core/formats/emd/emd.h>
#include <formats/mv.h>
#include <formats/gcad_multilayer.h>

#include <core/util/qt_helper.h>

#include <QTextEdit>

ComponentsScope::ComponentsScope()
{
  using namespace std;

  cout << "Initializing components..." << endl;

  // Models
  mFactory.Reg< models::ActionContainer >("models::ActionContainer");
  mFactory.Reg< models::Console         >("models::Console");
  mFactory.Reg< models::Mesh            >("models::Mesh");

  // Formats
  mFactory.Reg< formats::mv::Format              >("formats::mv::Format");
  mFactory.Reg< formats::gcad_multilayer::Format >("formats::gcad_multilayer::Format");
  
  // Widgets
  mFactory.Reg< widgets::MainFrame  >("widgets::MainFrame");
  mFactory.Reg< widgets::Mesh       >("widgets::Mesh");
  mFactory.Reg< widgets::Palette    >("widgets::Palette");
  mFactory.Reg< widgets::Navigation >("widgets::Navigation");
  mFactory.Reg< QTextEdit           >("QTextEdit");

  // util
  mFactory.Reg< util::QtHelper      >("util::QtHelper");
}

ComponentsScope::~ComponentsScope()
{
}

QObject * MakeComponent(const QString & CreatorID_)
{
  framework::ComponentFactory &factory = GRef< ComponentsScope >()->mFactory;

  std::cout << "Making component: " << CreatorID_.toStdString() << "...";

  QObject *pComponent = factory.Make< QObject >(CreatorID_);

  std::cout << (!pComponent ? "failed" : "ok") << std::endl;

  return pComponent;
}